package com.stone.customer.console.api.controller.v1;

import com.stone.customer.core.domain.entity.ComplaintsEntry;
import com.stone.customer.core.domain.repository.ComplaintsEntryRepository;
import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 用户投诉建议行表 管理 API
 *
 * @author Mr_wenpan@163.com 2021-07-19 21:54:57
 */
@RestController("complaintsEntryController.v1")
@RequestMapping("/v1/{organizationId}/complaints-entrys")
public class ComplaintsEntryController extends BaseController {

    @Autowired
    private ComplaintsEntryRepository complaintsEntryRepository;

    @ApiOperation(value = "用户投诉建议行表列表")
//    @Permission(level = ResourceLevel.ORGANIZATION)
    @Permission(permissionPublic = true, permissionLogin = true)
    @GetMapping
    public ResponseEntity<Page<ComplaintsEntry>> list(ComplaintsEntry complaintsEntry, @ApiIgnore @SortDefault(value = ComplaintsEntry.FIELD_COMPLAINTS_ENTRY_ID,
            direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<ComplaintsEntry> list = complaintsEntryRepository.pageAndSort(pageRequest, complaintsEntry);
        return Results.success(list);
    }

    @ApiOperation(value = "用户投诉建议行表明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{complaintsEntryId}")
    public ResponseEntity<ComplaintsEntry> detail(@PathVariable Long complaintsEntryId) {
        ComplaintsEntry complaintsEntry = complaintsEntryRepository.selectByPrimaryKey(complaintsEntryId);
        return Results.success(complaintsEntry);
    }

    @ApiOperation(value = "用户投诉建议行表明细")
    @GetMapping("/test/{complaintsEntryId}")
    public ResponseEntity<ComplaintsEntry> detail_test(@PathVariable Long complaintsEntryId) {
        ComplaintsEntry complaintsEntry = complaintsEntryRepository.selectByPrimaryKey(complaintsEntryId);
        return Results.success(complaintsEntry);
    }

    @ApiOperation(value = "创建用户投诉建议行表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<ComplaintsEntry> create(@RequestBody ComplaintsEntry complaintsEntry) {
        validObject(complaintsEntry);
        complaintsEntryRepository.insertSelective(complaintsEntry);
        return Results.success(complaintsEntry);
    }

    @ApiOperation(value = "修改用户投诉建议行表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PutMapping
    public ResponseEntity<ComplaintsEntry> update(@RequestBody ComplaintsEntry complaintsEntry) {
        SecurityTokenHelper.validToken(complaintsEntry);
        complaintsEntryRepository.updateByPrimaryKeySelective(complaintsEntry);
        return Results.success(complaintsEntry);
    }

    @ApiOperation(value = "删除用户投诉建议行表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<?> remove(@RequestBody ComplaintsEntry complaintsEntry) {
        SecurityTokenHelper.validToken(complaintsEntry);
        complaintsEntryRepository.deleteByPrimaryKey(complaintsEntry);
        return Results.success();
    }

}
